import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
import Basic from '../components/employee/Basic'
import Adv from '../components/employee/Adv'
import Testcase from '../components/fmc/Testcase'
import Pigfarm from '../components/fmc/Pigfarm'
import Script from '../components/fmc/Script'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/home' },
  { path: '/login', component: Login },
  {
    path: '/home',
    redirect: '/welcome',
    component: Home,
    children: [
      { path: '/welcome', component: Welcome }
    ]
  },
  {
    path: '/emp',
    redirect: 'basic',
    component: Home,
    children: [
      { path: '/emp/basic', component: Basic },
      { path: 'adv', component: Adv }
    ]
  }, {
    path: '/fmc',
    redirect: 'basic',
    component: Home,
    children: [
      { path: 'testcase', component: Testcase },
      { path: 'farm', component: Pigfarm },
      { path: 'script', component: Script }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 挂载路由导航守卫11
router.beforeEach((to, from, next) => {
  // to   将要访问的路径
  // from 代表从按个路径跳转而来
  // next 是一个函数, 表示放行
  //      next() 放行  next('/login') 强行跳转
  if (to.path === '/login') {
    return next()
  }
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) {
    return next('/login')
  }
  next()
})

export default router
